import { ref } from 'vue'
import { defineStore } from 'pinia'
import {useUserStore} from "@/stores/userStore.js";
export const useRouteStore = defineStore('useRouteStore', () => {
    const lastRoute = ref([])

    if (localStorage.getItem('routeStore')) {
        lastRoute.value = JSON.parse(localStorage.getItem('routeStore')).lastRoute
    }

    const setLastRoute = (fullPath) =>{
        if (lastRoute.value.length === 0) {
            lastRoute.value.push({
                uid: useUserStore().userInfo?.id,
                path: fullPath
            })
            return void 0
        }

        lastRoute.value.forEach((item, index)=> {
            if(item.uid === useUserStore().userInfo?.id) {
                lastRoute.value[index].path = fullPath
                return void 0
            }
        })
    }

    return {
        lastRoute,
        setLastRoute
    }
}, {
    persist: {
        key: 'routeStore',
        storage: localStorage
    }
})
